layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 管理
     */
    var ShiAdditionalFunction = {
        tableId: "shiAdditionalFunctionTable"
    };

    /**
     * 初始化表格的列
     */
    ShiAdditionalFunction.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '附加功能表，id'},
            {field: 'zeroSpace', sort: true, title: '零空间，0没有，1有，2选配'},
            {field: 'zeroGravity', sort: true, title: '零重力，0没有，1有，2选配'},
            {field: 'bluetoothApp', sort: true, title: '蓝牙APP，0没有，1有，2选配'},
            {field: 'workingTime', sort: true, title: '累计工作时间，0没有，1有，2选配'},
            {field: 'coins', sort: true, title: '投币功能，0没有，1有，2选配'},
            {field: 'weChatPay', sort: true, title: '微信支付，0没有，1有，2选配'},
            {field: 'automaticMode', sort: true, title: '自动模式，手工输入'},
            {field: 'memoryPattern', sort: true, title: '记忆模式，手工输入'},
            {field: 'safeguard', sort: true, title: '安全保护，0没有，1有，2选配'},
            {field: 'timeSetting', sort: true, title: '时间设定，0没有，1有，2选配'},
            {field: 'jadeStone', sort: true, title: '玉石热疗器，0没有，1有，2选配'},
            {field: 'machineLinkage', sort: true, title: '整机联动，0没有，1有，2选配'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ShiAdditionalFunction.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ShiAdditionalFunction.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    ShiAdditionalFunction.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/shiAdditionalFunction/add';
    };

    /**
     * 导出excel按钮
     */
    ShiAdditionalFunction.exportExcel = function () {
        var checkRows = table.checkStatus(ShiAdditionalFunction.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ShiAdditionalFunction.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/shiAdditionalFunction/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ShiAdditionalFunction.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/shiAdditionalFunction/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ShiAdditionalFunction.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ShiAdditionalFunction.tableId,
        url: Feng.ctxPath + '/shiAdditionalFunction/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ShiAdditionalFunction.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ShiAdditionalFunction.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ShiAdditionalFunction.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ShiAdditionalFunction.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ShiAdditionalFunction.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ShiAdditionalFunction.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ShiAdditionalFunction.onDeleteItem(data);
        }
    });
});
